#include "SipGDBodyAdaptor.hpp"
#include "SipStrUtils.hpp"

namespace wtsip{


#define SipBodyType(_TYPE) #_TYPE

const char* CSipGDBodyAdaptor::s_VarMark[MAXBodyType] = 
{
    SipBodyType(Catalog),
    SipBodyType(Alarm),
    SipBodyType(PTZCommand),
    SipBodyType(DeviceInfo),
    SipBodyType(EncoderSet),
    SipBodyType(TimeSet),
    SipBodyType(TimeGet),
    SipBodyType(FileList),
    SipBodyType(VOD),
    SipBodyType(PlanSet),
    SipBodyType(PlanList),
    SipBodyType(KeepAlive),
    SipBodyType(RealTimeKeepLive),
};

#undef SipBodyType

CSipGDBodyAdaptor::CSipGDBodyAdaptor()
{
}

CSipGDBodyAdaptor::~CSipGDBodyAdaptor()
{
}

CSipGDBodyAdaptor::EBodyType CSipGDBodyAdaptor::getMessageBodyType(SipMessage& msg)
{
    EBodyType t = UNKOWN;
    if (msg.getContents())
    {
        std::string body = msg.getContents()->getBodyData().c_str();
        int bIndex = body.find("<Variable>");
        int markLen = strlen("<Variable>");
        int eIndex = body.find("</Variable>", bIndex + markLen);
        if (bIndex >= 0 && eIndex >= 0)
        {
            int varLen = eIndex - bIndex - markLen;
            std::string var = body.substr(bIndex + markLen, varLen);
            CStrUntils::trim(var);
            for (int i = 0; i < MAXBodyType; i++)
            {
                if (strlen(s_VarMark[i]) == varLen 
                    && strcmp(s_VarMark[i], var.c_str()) == 0)
                {
                    t = (CSipGDBodyAdaptor::EBodyType)i;
                    break;
                }
            }
        }
    }
    return t;
}


bool CSipGDBodyAdaptor::isRegAlive(SipMessage& msg)
{
    return (getMessageBodyType(msg) == CSipGDBodyAdaptor::KeepAlive);
}


bool CSipGDBodyAdaptor::isRealAlive(SipMessage& msg)
{
    return (getMessageBodyType(msg) == CSipGDBodyAdaptor::RealTimeKeepLive);
}


//////////////////// check list response ///////////////////

bool CSipGDBodyAdaptor::listFileOver(SipMessage& msg)
{
    return checkMark(msg, std::string("<RemainFileNum>0</RemainFileNum>"));
}


bool CSipGDBodyAdaptor::listPlanOver(SipMessage& msg)
{
    return checkMark(msg, std::string("<RemainPlanNum>0</RemainPlanNum>"));
}


bool CSipGDBodyAdaptor::checkMark(SipMessage& msg, std::string& mark)
{
    bool rt = false;
    if (msg.getContents())
    {
        std::string body = msg.getContents()->getBodyData().c_str();
        int i = body.find(mark);
        if (i >= 0){
            rt = true;
        }
    }
    return rt;
}


//////////////////// create body methdos /////////////////////


std::string CSipGDBodyAdaptor::createRegAliveRequest(){
    return std::string("<?xml version=\"1.0\"?><Action><Notify><Variable>KeepAlive</Variable></Notify></Action>");
}


std::string CSipGDBodyAdaptor::createRegAliveResponse(){
    return std::string("<?xml version=\"1.0\"?><Response><Variable>KeepAlive</Variable><Result>0</Result></Response>");
}


std::string CSipGDBodyAdaptor::createRealAliveRequest(){
    return std::string("<?xml version=\"1.0\"?><Action><Notify><Variable>RealTimeKeepLive</Variable></Notify></Action>");
}


std::string CSipGDBodyAdaptor::createRealAliveResponse(){
    return std::string("<?xml version=\"1.0\"?><Response><Variable>RealTimeKeepLive</Variable><Result>0</Result></Response>");
}



}//namespace

